package com.example.test.chapter16;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.test.R;

public class PreferencesA extends Activity {
	private static final int EDIT_ID = Menu.FIRST + 2;

	private TextView checkbox = null;
	private TextView ringtone = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainpreferences);
		checkbox = (TextView) findViewById(R.id.checkboxpreference);
		ringtone = (TextView) findViewById(R.id.ringtonepreference);
		Button btn = (Button) findViewById(R.id.backpage);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),
						MainChapter16.class));
				finish();
			}
		});
	}

	@SuppressLint("UseValueOf")
	@Override
	protected void onResume() {
		super.onResume();
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false))
				.toString());
		ringtone.setText(prefs.getString("ringtone", " "));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit Prefs");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case EDIT_ID:
			startActivity(new Intent(getApplicationContext(),
					EditPreferences.class));
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
